package gold.digger;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1588 {
    public long startExecuteTime = System.currentTimeMillis();


    /**
     * 时间 4ms；空间 8 MB。
     * <p>
     * O(n) 的解法可以应对 10^6 乃至 10^7 的数字规模。如果这道题的数字规模是这样的，
     * 那么上面两种解法都将超时，这个问题也就变成了一个 Medium 甚至是 Hard 的问题
     */
    class Solution {
        public int sumOddLengthSubarrays(int[] arr) {
            int res = 0;
            for (int i = 0; i < arr.length; i++) {
                int left = i + 1, right = arr.length - i,
                        left_even = (left + 1) / 2, right_even = (right + 1) / 2,
                        left_odd = left / 2, right_odd = right / 2;
                res += (left_even * right_even + left_odd * right_odd) * arr[i];
            }
            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,4,2,5,3]");
        System.out.println(solution.sumOddLengthSubarrays(arr));
    }

    public static void main(String[] args) throws Exception {
        LC1588 an = new LC1588();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
